using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using ICP_MS.Plugin.Experiment.Properties;

namespace ICP_MS.Plugin.Experiment;

public class RegionSelectControl : UserControl, ISupportInitialize
{
	private bool isInitializing = false;

	private double massStart = 0.0;

	private double massEnd = 260.0;

	private MassRegionList deadRegionList = new MassRegionList();

	private MassRegionList selectedregionList = new MassRegionList();

	private MouseAction action = MouseAction.Add;

	private int leftBorder = 60;

	private int rightBorder = 20;

	private StringFormat tickTxtFormat = new StringFormat();

	private Pen linePen = new Pen(Color.Black);

	private Brush deadBrush = new SolidBrush(Color.Red);

	private Brush regionBrush = new SolidBrush(Color.Cyan);

	private Brush mouseBrush = new SolidBrush(Color.FromArgb(128, Color.Green));

	private Point curentPoint = Point.Empty;

	private Point mouseDown = Point.Empty;

	private Point mouseMove = Point.Empty;

	private bool bChangingRegion = false;

	private bool isMouseDown = false;

	private double mainTickHeight = 8.0;

	private double viceTickHeight = 5.0;

	private int mainGridCount = 26;

	private IContainer components = null;

	private PictureBox pictureBox1;

	private Label label1;

	private Label label2;

	private PictureBox pictureBox2;

	public IList<PointF> DeadRegionList
	{
		set
		{
			if (value == null || value.Count <= 0)
			{
				return;
			}
			deadRegionList.Clear();
			foreach (PointF region in value)
			{
				deadRegionList.Add(new MassRegion(region.X, region.Y));
			}
			if (!isInitializing)
			{
				Invalidate();
			}
		}
	}

	public MassRegionList SelectedRegionList => selectedregionList;

	public IList<ScanRegions> SelectedRegions
	{
		set
		{
			if (value == null || value.Count <= 0)
			{
				return;
			}
			selectedregionList.Clear();
			foreach (ScanRegions region in value)
			{
				selectedregionList.Add(new MassRegion(region.start, region.end));
			}
			selectedregionList.AdjustRegion();
			if (!isInitializing)
			{
				Invalidate();
				MassChanged();
			}
		}
	}

	private double MassLength => massEnd - massStart;

	private double LineLength => base.Width - rightBorder - leftBorder;

	public event EventHandler MassRegionChanged;

	public void BeginInit()
	{
		isInitializing = true;
	}

	public void EndInit()
	{
		isInitializing = false;
		Invalidate();
	}

	public void InitFullRegios(double start, double end)
	{
		ProcessRegion(new MassRegion(start, end), MouseAction.Add);
	}

	public RegionSelectControl()
	{
		InitializeComponent();
		SetStyle(ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, value: true);
		ApplyResources();
	}

	private void ApplyResources()
	{
		label1.Text = Lang.Txt("RegionSelectControl.Exclude.Text");
		label2.Text = Lang.Txt("RegionSelectControl.Include.Text");
	}

	private void RegionSelectControl_Paint(object sender, PaintEventArgs e)
	{
		if (isInitializing)
		{
			return;
		}
		Graphics g = e.Graphics;
		List<RectangleF> selectedRegionRects = new List<RectangleF>();
		foreach (MassRegion k in selectedregionList)
		{
			float left3 = (float)((double)leftBorder + (k.Left - massStart) / MassLength * LineLength);
			float width2 = (float)((k.Right - k.Left) / MassLength * LineLength);
			selectedRegionRects.Add(new RectangleF(left3, (float)(base.Height / 2) - (float)viceTickHeight * 2f, width2, (float)viceTickHeight * 2f));
		}
		if (selectedRegionRects.Count > 0)
		{
			g.FillRectangles(regionBrush, selectedRegionRects.ToArray());
			g.DrawRectangles(linePen, selectedRegionRects.ToArray());
		}
		List<RectangleF> deadRegionRects = new List<RectangleF>();
		foreach (MassRegion j in deadRegionList)
		{
			float left2 = (float)((double)leftBorder + (j.Left - massStart) / MassLength * LineLength);
			float width = (float)((j.Right - j.Left) / MassLength * LineLength);
			deadRegionRects.Add(new RectangleF(left2, (float)(base.Height / 2) - (float)viceTickHeight * 2f, width, (float)viceTickHeight * 2f));
		}
		if (deadRegionRects.Count > 0)
		{
			g.FillRectangles(deadBrush, deadRegionRects.ToArray());
			g.DrawRectangles(linePen, deadRegionRects.ToArray());
		}
		g.DrawLine(linePen, leftBorder, base.Height / 2, base.Width - rightBorder, base.Height / 2);
		float w = (float)(LineLength / (double)mainGridCount);
		for (int i = 0; i <= mainGridCount; i++)
		{
			float graduate = (float)((i % 5 == 0) ? mainTickHeight : viceTickHeight);
			e.Graphics.DrawLine(linePen, (float)leftBorder + (float)i * w, base.Height / 2, (float)leftBorder + (float)i * w, (float)(base.Height / 2) - graduate);
			if (i % 5 == 0)
			{
				float mass2 = (float)((double)i * MassLength / (double)mainGridCount);
				e.Graphics.DrawString(mass2.ToString(), new Font("SimSun", 10.5f), new SolidBrush(Color.Black), (float)leftBorder + (float)i * w - 10f, (float)(base.Height / 2) + Font.Size);
			}
		}
		if (bChangingRegion && mouseMove != Point.Empty)
		{
			float left = ((mouseDown.X < mouseMove.X) ? mouseDown.X : mouseMove.X);
			float rw = Math.Abs(mouseMove.X - mouseDown.X);
			g.FillRectangle(mouseBrush, left, (float)(base.Height / 2) - (float)viceTickHeight * 2f, rw, (float)viceTickHeight * 2f);
		}
		if (curentPoint != Point.Empty)
		{
			double mass = massStart + (double)(curentPoint.X - leftBorder) / LineLength * MassLength;
			e.Graphics.DrawString(mass.ToString("F2"), new Font("SimSun", 10.5f), new SolidBrush(Color.Red), curentPoint.X, (float)(base.Height / 2) + Font.Size);
		}
	}

	private void RegionSelectControl_MouseMove(object sender, MouseEventArgs e)
	{
		if (e.X >= leftBorder && e.X <= base.Width - rightBorder)
		{
			curentPoint = e.Location;
			if (action == MouseAction.Add)
			{
				Cursor = new Cursor(new MemoryStream(Resources.AddCursor));
			}
			else
			{
				Cursor = new Cursor(new MemoryStream(Resources.RemoveCursor));
			}
			if (isMouseDown)
			{
				mouseMove = e.Location;
			}
			else
			{
				mouseMove = Point.Empty;
			}
			Invalidate();
		}
		else
		{
			curentPoint = Point.Empty;
			Cursor = Cursors.Default;
		}
	}

	private void RegionSelectControl_MouseUp(object sender, MouseEventArgs e)
	{
		if (bChangingRegion && e.Button == MouseButtons.Left)
		{
			isMouseDown = false;
			double regLeft = massStart + (double)(mouseDown.X - leftBorder) / LineLength * MassLength;
			double regRight = massStart + (double)(e.X - leftBorder) / LineLength * MassLength;
			if (regLeft > regRight)
			{
				double reg = regLeft;
				regLeft = regRight;
				regRight = reg;
			}
			if (regRight > massEnd)
			{
				regRight = massEnd;
			}
			if (regLeft < massStart)
			{
				regLeft = massStart;
			}
			if (regLeft == regRight)
			{
				return;
			}
			MassRegion tempRegion = new MassRegion(regLeft, regRight);
			ProcessRegion(tempRegion);
		}
		bChangingRegion = false;
		Refresh();
	}

	private void ProcessRegion(MassRegion region, MouseAction act = MouseAction.None)
	{
		if (act == MouseAction.None)
		{
			act = action;
		}
		switch (act)
		{
		case MouseAction.Add:
			selectedregionList.AddRegion(region);
			break;
		case MouseAction.Remove:
			selectedregionList.RemoveRegion(region);
			break;
		}
		selectedregionList.RemoveRegionList(deadRegionList);
		MassChanged();
		Refresh();
	}

	private void MassChanged()
	{
		if (this.MassRegionChanged != null)
		{
			this.MassRegionChanged(selectedregionList, null);
		}
	}

	private void RegionSelectControl_MouseDown(object sender, MouseEventArgs e)
	{
		if (e.Button == MouseButtons.Right)
		{
			ContextMenu menu = new ContextMenu();
			menu.MenuItems.Add(Lang.Txt("FormScanLA.Add"), delegate
			{
				action = MouseAction.Add;
			});
			menu.MenuItems.Add(Lang.Txt("FormScanLA.Delete"), delegate
			{
				action = MouseAction.Remove;
			});
			if (action == MouseAction.Add)
			{
				menu.MenuItems[0].Checked = true;
			}
			else
			{
				menu.MenuItems[1].Checked = true;
			}
			menu.Show(this, e.Location);
		}
		else if (e.X >= leftBorder && e.X <= base.Width - rightBorder)
		{
			isMouseDown = true;
			mouseDown = e.Location;
			bChangingRegion = true;
		}
	}

	private void RegionSelectControl_MouseEnter(object sender, EventArgs e)
	{
	}

	private void RegionSelectControl_MouseLeave(object sender, EventArgs e)
	{
		Cursor = Cursors.Arrow;
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.pictureBox1 = new System.Windows.Forms.PictureBox();
		this.label1 = new System.Windows.Forms.Label();
		this.label2 = new System.Windows.Forms.Label();
		this.pictureBox2 = new System.Windows.Forms.PictureBox();
		((System.ComponentModel.ISupportInitialize)this.pictureBox1).BeginInit();
		((System.ComponentModel.ISupportInitialize)this.pictureBox2).BeginInit();
		base.SuspendLayout();
		this.pictureBox1.BackColor = System.Drawing.Color.Red;
		this.pictureBox1.Location = new System.Drawing.Point(10, 1);
		this.pictureBox1.Name = "pictureBox1";
		this.pictureBox1.Size = new System.Drawing.Size(28, 11);
		this.pictureBox1.TabIndex = 0;
		this.pictureBox1.TabStop = false;
		this.label1.AutoSize = true;
		this.label1.Location = new System.Drawing.Point(9, 12);
		this.label1.Name = "label1";
		this.label1.Size = new System.Drawing.Size(29, 12);
		this.label1.TabIndex = 1;
		this.label1.Text = "无效";
		this.label2.AutoSize = true;
		this.label2.Location = new System.Drawing.Point(9, 39);
		this.label2.Name = "label2";
		this.label2.Size = new System.Drawing.Size(29, 12);
		this.label2.TabIndex = 3;
		this.label2.Text = "扫描";
		this.pictureBox2.BackColor = System.Drawing.Color.Cyan;
		this.pictureBox2.Location = new System.Drawing.Point(9, 26);
		this.pictureBox2.Name = "pictureBox2";
		this.pictureBox2.Size = new System.Drawing.Size(28, 11);
		this.pictureBox2.TabIndex = 2;
		this.pictureBox2.TabStop = false;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.Controls.Add(this.label2);
		base.Controls.Add(this.pictureBox2);
		base.Controls.Add(this.label1);
		base.Controls.Add(this.pictureBox1);
		this.DoubleBuffered = true;
		base.Name = "RegionSelectControl";
		base.Size = new System.Drawing.Size(729, 52);
		base.Paint += new System.Windows.Forms.PaintEventHandler(RegionSelectControl_Paint);
		base.MouseDown += new System.Windows.Forms.MouseEventHandler(RegionSelectControl_MouseDown);
		base.MouseEnter += new System.EventHandler(RegionSelectControl_MouseEnter);
		base.MouseLeave += new System.EventHandler(RegionSelectControl_MouseLeave);
		base.MouseMove += new System.Windows.Forms.MouseEventHandler(RegionSelectControl_MouseMove);
		base.MouseUp += new System.Windows.Forms.MouseEventHandler(RegionSelectControl_MouseUp);
		((System.ComponentModel.ISupportInitialize)this.pictureBox1).EndInit();
		((System.ComponentModel.ISupportInitialize)this.pictureBox2).EndInit();
		base.ResumeLayout(false);
		base.PerformLayout();
	}
}
